package com.nut.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nut.course.common.Result;
import com.nut.course.entity.PartnerTeacher;
import com.nut.course.mapper.BizPartnerTeacherMapper;
import com.nut.course.service.PartnerTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Set;

@Slf4j
@Service
public class PartnerTeacherServiceImpl implements PartnerTeacherService {

    @Autowired
    private BizPartnerTeacherMapper bizPartnerTeacherMapper;

    @Override
    public Result<IPage<PartnerTeacher>> list(Integer partnerId, String name, Integer pageNo, Integer pageSize) {
        Page<PartnerTeacher> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);

//        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        wrapper.select(User.class, info -> !info.getColumn().equals("create_time")
//                && !info.getColumn().equals("manager_id")).in("age", Arrays.asList(18, 31, 30, 34, 35)).last("limit 1");
//        List<User> users = userMapper.selectList(wrapper);

        LambdaQueryWrapper<PartnerTeacher> queryWrapper = new LambdaQueryWrapper<>();
        //排除 profile
        queryWrapper.select(PartnerTeacher.class, a -> !a.getColumn().equals("profile"));
        if (partnerId != null) {
            queryWrapper.eq(PartnerTeacher::getPartnerId, partnerId);
        }
        if (StringUtils.hasLength(name)) {
            queryWrapper.like(PartnerTeacher::getName, name);
        }
        queryWrapper.orderByDesc(PartnerTeacher::getId);

        IPage<PartnerTeacher> list = bizPartnerTeacherMapper.selectPage(page, queryWrapper);
        return Result.ok(list);
    }

    @Override
    public Result<PartnerTeacher> getById(Integer id) {
        PartnerTeacher partnerTeacher = bizPartnerTeacherMapper.selectById(id);
        return Result.ok(partnerTeacher);
    }

    @Override
    public Result add(PartnerTeacher partnerTeacher) {


        bizPartnerTeacherMapper.insert(partnerTeacher);
        return Result.ok();
    }

    @Override
    public Result updateById(PartnerTeacher partnerTeacher) {
        bizPartnerTeacherMapper.updateById(partnerTeacher);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        for (Integer id : ids) {
            bizPartnerTeacherMapper.deleteById(id);
        }
        return Result.ok();
    }
}
